package com.tanhua.dubbo.huanxin.utils;

import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;

public class MailUtils {
	private static String from = "hcy_1521900@163.com";// 用于给用户发送邮件的邮箱
	private static String password = "PXQSWUTNVRFSXEJI"; // 邮箱的密码
	private static String host = "smtp.163.com";// 发送邮件的服务器地址
	public static void sendMail(String to, String title, String content) {
		try {
			Properties prop = new Properties();
			prop.setProperty("mail.host", host);
			prop.setProperty("mail.transport.protocol", "smtp");
			prop.setProperty("mail.smtp.auth", "true");
			Session session = Session.getInstance(prop);
			session.setDebug(true);
			Transport ts = session.getTransport();
			ts.connect(host, from, password);
			MimeMessage message = new MimeMessage(session);
			message.setFrom(new InternetAddress(from));
			message.setRecipient(Message.RecipientType.TO, new InternetAddress(
					to));
			message.setSubject(title);
			message.setContent(content, "text/html;charset=UTF-8");
			message.saveChanges();
			ts.sendMessage(message, message.getAllRecipients());
			ts.close();
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}
}
